package com.ai.planttrees.service.impl;

import com.ai.planttrees.dao.PrizeMapper;
import com.ai.planttrees.dao.PrizeRecordMapper;
import com.ai.planttrees.dao.UserTreeMapper;
import com.ai.planttrees.model.Prize;
import com.ai.planttrees.model.PrizeRecord;
import com.ai.planttrees.model.UserTree;
import com.ai.planttrees.service.PrizeService;
import com.ai.planttrees.core.AbstractService;
import com.ai.planttrees.util.BaseUtil;
import com.ai.planttrees.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by CodeGenerator on 2018/04/24.
 */
@Service
public class PrizeServiceImpl extends AbstractService<Prize> implements PrizeService {
    private Logger logger = LoggerFactory.getLogger(PrizeServiceImpl.class);

    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PrizeRecordMapper prizeRecordMapper;
    @Resource
    private UserTreeMapper userTreeMapper;

    /**
     * 添加奖品兑换记录
     * @param prizeRecord
     * @return 成功兑换记录数
     */
    @Override
    @Transactional
    public Result addPrizeRecords(PrizeRecord prizeRecord) {
        //查询奖品余量
        Prize p = prizeMapper.selectByPrimaryKey(prizeRecord.getPrizeId());
        if(p.getPrizeTotal() <= 0){
            return Result.fail("奖品已全部兑换完");
        }
        //查询是否兑换
        PrizeRecord prizeRecord1 = prizeRecordMapper.selectOne(prizeRecord);
        if(prizeRecord1!=null){
            return Result.fail("用户已兑换过");
        }
        //计算余量是否够兑换
        logger.info("获取兑换的奖品对象");
        Prize prize = prizeMapper.selectByPrimaryKey(prizeRecord.getPrizeId());
        logger.info("获取结果：{}",prize);
        logger.info("获取兑换奖品的用户活动信息");
        UserTree uu = new UserTree();
        uu.setPhone(prizeRecord.getUserId());
        UserTree userTree = userTreeMapper.selectOne(uu);
        logger.info("获取结果：{}",userTree);
        logger.info("用奖品的流量值与用户的活动流量值相减，{}-{}",userTree.getFlow() , prize.getPrizeValue());
        long l = userTree.getFlow() - prize.getPrizeValue();
        logger.info("结果：{}",l);
        if(l < 0){
            return Result.fail("用户流量活动流量值不足");
        }
        p.setPrizeTotal(p.getPrizeTotal()-1);
        int z = prizeMapper.updateByPrimaryKeySelective(p);
        logger.info("更新奖品数量，结果：{}",z);
        if(z==1){
            userTree.setFlow(l);
            prizeRecord.setId(BaseUtil.getUUID());
            int j = userTreeMapper.updateByPrimaryKeySelective(userTree);//更新用户流量
            logger.info("更新用户流量，结果：{}",j);
            int i = prizeRecordMapper.insertSelective(prizeRecord);//插入兑换记录
            logger.info("插入兑换记录，结果：{}",i);
        }
        return Result.success(prize.getPrizeValue());
    }

    /**
     * 按用户活动主键查询此用户兑换记录
     * @param userId
     * @return 全部奖品集合
     */
    @Override
    @Transactional
    public Result selByUserId(String userId) {
        logger.info("按用户活动id查询兑奖记录，userId：{}",userId);
        List<Prize> prizeList = prizeMapper.selByUserId(userId);
        logger.info("结果：{}",prizeList);
        Result result = Result.success(prizeList);
        return result;
    }




}
